/*
* $Id: UserFieldForm.java 219 2010-12-19 13:51:18Z volok-aleksej@yandex.ru $
*
* Copyright 2008-2010 by Volokitin Alexey
* All rights reserved
*
*/
package apiary.ui.journal;

import apiary.db.managers.ConvertData;
import apiary.component.journal.Wizard;
import apiary.db.managers.DBManager;
import apiary.db.UserFieldsStore.UserFieldsRecord;
import java.util.Date;

import java.util.Vector;
import javax.microedition.lcdui.*;

public abstract class UserFieldForm extends WizardForm
{
    public class UserField
    {
        public String nameField;
        public String valueField;
        public int typeField;
    }

    public UserFieldForm(String name, Wizard wiz, Integer id, boolean nextCommand) {
        super(name, wiz, id, nextCommand);
    }

    protected void setUserTable(int table) {
        userTable = table;
    }
    
    protected void appendUserFields() {
        Vector userFields = DBManager.getInstance().getUserFields(userTable);
        for(int i = 0; i < userFields.size(); i++) {
            UserFieldsRecord userField = (UserFieldsRecord) userFields.elementAt(i);
            if(userField.typeField == UserFieldsRecord.TYPE_DOUBLE) {
                append(new TextField(userField.nameField, "", 256, TextField.DECIMAL));
            } else if (userField.typeField == UserFieldsRecord.TYPE_INTEGER) {
                append(new TextField(userField.nameField, "", 256, TextField.NUMERIC));
            } else if(userField.typeField == UserFieldsRecord.TYPE_STRING){
                append(new TextField(userField.nameField, "", 256, TextField.ANY));
            } else if(userField.typeField == UserFieldsRecord.TYPE_DATE){
                append(new DateField(userField.nameField, DateField.DATE));
            } else if(userField.typeField == UserFieldsRecord.TYPE_BOOL) {
                ChoiceGroup choice = new ChoiceGroup("", ChoiceGroup.MULTIPLE);
                choice.append(userField.nameField, null);
                append(choice);
            }
        }
    }

    protected void setUserFieldsValue(int userBegin, Vector userData) {
        Vector userFields = DBManager.getInstance().getUserFields(userTable);
        for(int i = 0; i < userFields.size(); i++) {
            UserFieldsRecord userField = (UserFieldsRecord) userFields.elementAt(i);
            
            String userFieldValue = "";
            for(int j = 0; j < userData.size(); j++) {
                UserField field = (UserField) userData.elementAt(j);
                if(field.nameField.equals(userField.nameField)) {
                    userFieldValue = field.valueField;
                    break;
                }
            }

            Item itemField = get(userBegin);
            if(userField.typeField == UserFieldsRecord.TYPE_DOUBLE ||
               userField.typeField == UserFieldsRecord.TYPE_INTEGER ||
               userField.typeField == UserFieldsRecord.TYPE_STRING) {
                ((TextField)itemField).setString(userFieldValue);
            } else if(userField.typeField == UserFieldsRecord.TYPE_DATE){
                Date date = new Date(Long.parseLong(userFieldValue));
                ((DateField)itemField).setDate(date);
            } else if(userField.typeField == UserFieldsRecord.TYPE_BOOL) {
                ((ChoiceGroup)itemField).setSelectedIndex(0, userFieldValue.equals("true"));
            }
            userBegin++;
        }
    }

    protected Vector getUserFieldsValue(int userBegin)
    {
        Vector value = new Vector();
        Vector userFields = DBManager.getInstance().getUserFields(UserFieldsRecord.TABLE_POINTS);
        for(int i = userBegin; i < size(); i++) {
            UserFieldsRecord userFieldRec = (UserFieldsRecord) userFields.elementAt(i - userBegin);
            UserField userField = new UserField();
            ConvertData.UserFieldsToUserField(userFieldRec, userField);
            if(userFieldRec.typeField == UserFieldsRecord.TYPE_DOUBLE ||
               userFieldRec.typeField == UserFieldsRecord.TYPE_INTEGER ||
               userFieldRec.typeField == UserFieldsRecord.TYPE_STRING) {
                TextField valueUserField = (TextField) get(i);
                userField.valueField = valueUserField.getString();
            } else if(userFieldRec.typeField == UserFieldsRecord.TYPE_DATE){
                DateField valueUserField = (DateField) get(i);
                userField.valueField = String.valueOf(valueUserField.getDate().getTime());
            } else if(userFieldRec.typeField == UserFieldsRecord.TYPE_BOOL) {
                ChoiceGroup valueUserField = (ChoiceGroup) get(i);
                userField.valueField = valueUserField.isSelected(0) ? "true" : "false";
            }
            value.addElement(userField);
        }
        return value;
    }

    private int userTable;
}
